<template>
  <a-modal :title="isEdit ? '编辑' : '新增'" :width="560" :visible="visible" @ok="handleModalConfirm"
    @cancel="handleModalCancel" :confirm-loading="modalLoading">
    <a-form-model ref="form" :model="form" :rules="formRules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
      <a-form-model-item label="渠道Code" required>
        <a-input v-model="form.channelCode" placeholder="请输入" allowClear disabled />
      </a-form-model-item>
      <a-form-model-item label="渠道名称" prop="channelName">
        <a-input v-model="form.channelName" placeholder="请输入" allowClear />
      </a-form-model-item>
      <a-form-model-item label="公司名称" prop="companyName">
        <a-input v-model="form.companyName" placeholder="请输入" allowClear />
      </a-form-model-item>
      <a-form-model-item v-if="!isEdit" label="助贷开关" required>
        <a-switch v-model="form.loanAssistanceStatus" checked-children="开启" un-checked-children="关闭"
          style="width:100px" />
      </a-form-model-item>
      <a-form-model-item label="价格" prop="price">
        <a-input-number v-model="form.price" :min="0" placeholder="请输入" style="width: 100%" />
      </a-form-model-item>
      <a-form-model-item v-if="!isEdit" label="分润模式" prop="shareProfit">
        <a-select v-model="form.shareProfit" :options="shareOptions" placeholder="请选择" allowClear></a-select>
      </a-form-model-item>
      <a-form-model-item v-if="!isEdit" label="渠道类型" prop="type">
        <a-select v-model="form.type" :options="typeOptions" placeholder="请选择" allowClear></a-select>
      </a-form-model-item>
      <a-form-model-item label="权限">
        <a-input v-model="form.apiAuthority" placeholder="请输入" allowClear />
      </a-form-model-item>
      <a-form-model-item label="密钥">
        <a-input v-model="form.secretKey" placeholder="请输入" allowClear />
      </a-form-model-item>
      <a-form-model-item label="备注">
        <a-textarea v-model="form.remark" placeholder="请输入" :auto-size="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import * as Ajax from '@/api/channel';
export default {
  props: {
    visible: {
      type: Boolean,
    },
    default: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      modalLoading: false,
      form: {
        typeOptions: '',
        secretKey: '',
        channelCode: '',
        channelName: '',
        loanAssistanceStatus: false,
        price: '',
        shareProfit: undefined,
        type: undefined,
        remark: '',
      },
      formRules: {
        channelName: { required: true, message: '请输入渠道名称', trigger: ['blur', 'change'] },
        companyName: { required: true, message: '请输入公司名称', trigger: ['blur', 'change'] },
        price: { required: true, message: '请输入价格', trigger: ['blur', 'change'] },
        shareProfit: { required: true, message: '请选择分润模式', trigger: ['blur', 'change'] },
        type: { required: true, message: '请选择渠道类型', trigger: ['blur', 'change'] },
      },
      typeOptions: [
        { label: '同业', value: 0 },
        { label: '信息流', value: 1 },
      ],
      shareOptions: [
        { label: '单价', value: 0 },
        { label: '分润', value: 1 },
      ],
    };
  },
  computed: {
    isEdit() {
      return Object.keys(this.default).length > 0;
    },
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.isEdit) {
          this.form = JSON.parse(JSON.stringify(this.default));
        } else {
          this.getChannelCode();
        }
      } else {
        // this.handleModalCancel();
      }
    },
  },
  methods: {
    getChannelCode() {
      Ajax.createChannelList().then(({ success, data }) => {
        if (success) {
          this.form.channelCode = data;
        }
      });
    },
    handleModalConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.modalLoading = true;
          const params = { ...this.form };
          params.loanAssistanceStatus = params.loanAssistanceStatus ? 0 : 1;
          const ajaxName = this.isEdit ? 'editChannelList' : 'addChannelList';
          const message = this.isEdit ? '编辑成功' : '新增成功';
          Ajax[ajaxName](params)
            .then(({ success }) => {
              if (success) {
                this.$message.success(message);
                this.$emit('success');
                this.handleModalCancel();
              }
            })
            .finally(() => {
              this.modalLoading = false;
            });
        }
      });
    },
    handleModalCancel() {
      this.form = this.$options.data().form;
      this.$refs.form.resetFields();
      this.$emit('close');
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 20px;
  padding-top: 16px;
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;
}

::v-deep .ant-form-item {
  &:last-child {
    margin-bottom: 0;
  }
}
</style>
